package com.tengju.data.infrastructure.repository;

import com.tengju.data.domain.model.user.UserStarFeature;
import com.tengju.data.domain.model.user.UserStarFeatureSnapshotRepository;
import com.tengju.data.infrastructure.persistence.mybatis.dataobject.StarUserFeatureMonthDO;
import com.tengju.data.infrastructure.persistence.mybatis.mapper.tengju.custom.StarUserFeatureMonthDOCustomMapper;
import com.tengju.data.infrastructure.repository.convert.StarFeatureConvert;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chenhang
 * @Date 2021/1/22
 */
@Repository
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class UserStarFeatureSnapshotRepositoryImpl implements UserStarFeatureSnapshotRepository {

    private final StarUserFeatureMonthDOCustomMapper starUserFeatureMonthDOCustomMapper;

    @Override
    public List<UserStarFeature> listMonthStarFeature(Integer month) {
        List<StarUserFeatureMonthDO> starUserFeatureMonthDOList = starUserFeatureMonthDOCustomMapper.listMonthStarInfo(month);
        if (CollectionUtils.isEmpty(starUserFeatureMonthDOList)) {
            return Collections.emptyList();
        }
        return starUserFeatureMonthDOList.stream()
                .map(StarFeatureConvert::convertSnapshotToModel)
                .collect(Collectors.toList());
    }

    @Override
    public void saveStarFeatureMonthSnapshot(Integer month, List<UserStarFeature> userStarFeatureList) {
        if (CollectionUtils.isEmpty(userStarFeatureList)) {
            return;
        }
        starUserFeatureMonthDOCustomMapper.deleteByMonth(month);
        List<StarUserFeatureMonthDO> starUserFeatureMonthDOList = userStarFeatureList.stream()
                .map(v -> StarFeatureConvert.convertToDo(v,month))
                .collect(Collectors.toList());
        starUserFeatureMonthDOCustomMapper.batchInsertWithMonth(month,starUserFeatureMonthDOList);
    }
}
